#!/usr/bin/env python

from bs4 import BeautifulSoup
import urllib2
import re
import sys

UW_ID_LOW  = 4478997
UW_ID_HIGH = 4482320
# We may have mis-estimated the upper and lower bounds of the UW bookstore ID
# range for this semester. Fudge the bounds a bit.
UW_ID_FUDGE = 1000
UW_ID_DELIM = '%2C'
ISBN_REGEX = re.compile('"isbn":"([0-9]+)"')
BATCH_SIZE = 10
MAX_BATCHES = 10000

def MakeUrl(uw_ids):
  return ('http://compare.uwbookstore.com/comparison?id=' +
          UW_ID_DELIM.join([str(s) for s in uw_ids]))

def FetchPage(uw_ids):
  return urllib2.urlopen(MakeUrl(uw_ids)).read()

def GetIsbns(uw_ids):
  isbns = set()
  html = FetchPage(uw_ids)
  for match in re.finditer(ISBN_REGEX, html):
    isbns.add(match.group(1))
  return isbns

def GetIsbnRange(uw_id_low, uw_id_high):
  return GetIsbns(range(uw_id_low, uw_id_high + 1))

def main():
  isbns = set()
  start = UW_ID_LOW - UW_ID_FUDGE
  end = UW_ID_HIGH + UW_ID_FUDGE
  batches = 0
  while start < end:
    batch_end = min(end, start + BATCH_SIZE - 1)
    batch_isbns = GetIsbnRange(start, batch_end)
    isbns |= batch_isbns
    start = batch_end + 1
    batches += 1
    sys.stderr.write('Fetched batch [%i, %i]. Got %i ISBNs.\n' %
                     (start, batch_end, len(batch_isbns)))
    if batches >= MAX_BATCHES:
      break

  for i in isbns:
    print i

if __name__ == '__main__':
  main()

